<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>http | 樱桃小窝</title><meta name="keywords" content="http,网络"><meta name="author" content="樱桃先生"><meta name="copyright" content="樱桃先生"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Http什么是Http?HTTP(HyperText Transfer Protocol) 超文本传输协议.  HTTP&#x2F;0.9 单行协议1989年 当时还在欧洲核子研究组织工作的蒂姆伯纳斯李(Tim Berners-Lee)提出了一种能让远隔两地的研究者们共享知识的设想,最开始称为Mesh,后来在1990年实施期间将其命名为 World Wide Web(万维网) 它基于现有的TCP&#x2F;IP协议构">
<meta property="og:type" content="article">
<meta property="og:title" content="http">
<meta property="og:url" content="http://example.com/posts/374aa28c/index.html">
<meta property="og:site_name" content="樱桃小窝">
<meta property="og:description" content="Http什么是Http?HTTP(HyperText Transfer Protocol) 超文本传输协议.  HTTP&#x2F;0.9 单行协议1989年 当时还在欧洲核子研究组织工作的蒂姆伯纳斯李(Tim Berners-Lee)提出了一种能让远隔两地的研究者们共享知识的设想,最开始称为Mesh,后来在1990年实施期间将其命名为 World Wide Web(万维网) 它基于现有的TCP&#x2F;IP协议构">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/blog-common.png">
<meta property="article:published_time" content="2021-03-25T16:39:00.000Z">
<meta property="article:modified_time" content="2022-08-27T09:44:46.542Z">
<meta property="article:author" content="樱桃先生">
<meta property="article:tag" content="http">
<meta property="article:tag" content="网络">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/blog-common.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/posts/374aa28c/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'http',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-08-27 17:44:46'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://s2.loli.net/2022/08/20/hk9Zvq4YRsIcnrw.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">78</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">65</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间线</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/gallery/"><i class="fa-fw fas fa-camera"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fas fa-comment-dots"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/blog-common.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">樱桃小窝</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间线</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/gallery/"><i class="fa-fw fas fa-camera"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fas fa-comment-dots"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">http</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-03-25T16:39:00.000Z" title="发表于 2021-03-26 00:39:00">2021-03-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-08-27T09:44:46.542Z" title="更新于 2022-08-27 17:44:46">2022-08-27</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF/%E7%BD%91%E7%BB%9C/">网络</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="http"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Http"><a href="#Http" class="headerlink" title="Http"></a>Http</h1><h2 id="什么是Http"><a href="#什么是Http" class="headerlink" title="什么是Http?"></a>什么是Http?</h2><p>HTTP(HyperText Transfer Protocol) 超文本传输协议. </p>
<h2 id="HTTP-0-9-单行协议"><a href="#HTTP-0-9-单行协议" class="headerlink" title="HTTP/0.9 单行协议"></a>HTTP/0.9 单行协议</h2><p>1989年 当时还在欧洲核子研究组织工作的蒂姆伯纳斯李(Tim Berners-Lee)提出了一种能让远隔两地的研究者们共享知识的设想,最开始称为Mesh,后来在1990年实施期间将其命名为 World Wide Web(万维网) 它基于现有的TCP/IP协议构建,包括四个部分:</p>
<ul>
<li>一种表示超文本文档的文本格式,即<strong>超文本标记语言</strong>(HTML);</li>
<li>一种用于交换这些文档的简单协议,即<strong>HyperText传输协议</strong>(HTTP);</li>
<li>一个<strong>客户端</strong>可以显示这些文档,第一个Web浏览器称为World Wide Web</li>
<li>一个可以访问文档的<strong>服务器</strong></li>
</ul>
<p>这四部分在1990年底完成,虽然这时候Web页面只能显示单纯的文本内容,但是已经基本满足了建立Web站点的初衷,实现了<strong>信息资源共享</strong></p>
<p>以下就是HTTP/0.9的请求内容:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET /page.html</span><br></pre></td></tr></table></figure>

<p>用唯一可用的GET方法向目标服务器获取指定的文档(一旦连接到服务器,协议,服务器,端口号这些都不是必须的)</p>
<p>响应也机器简单 只包含文档本身</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">网页的内容</span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>一旦出现问题,一个特殊的包含问题描述信息的HTML文件将被发回,供人们查看</p>
<h2 id="HTTP-1-0-构建可扩展性"><a href="#HTTP-1-0-构建可扩展性" class="headerlink" title="HTTP/1.0 构建可扩展性"></a>HTTP/1.0 构建可扩展性</h2><ul>
<li>协议版本信息会随着每一次请求发送</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">-------HTTP/0.9请求--------</span><br><span class="line">GET /page.html</span><br><span class="line"></span><br><span class="line">-------HTTP/1.0请求--------</span><br><span class="line">GET /page.html HTTP/1.0 -&gt;新增协议版本</span><br></pre></td></tr></table></figure>

<ul>
<li>服务器在响应时回复状态码,使浏览器能了解请求执行成功或失败,并相应调整行为(如更新或失败)</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">-------HTTP/0.9响应--------</span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">...</span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line">-------HTTP/1.0响应--------</span><br><span class="line">200 OK -&gt; 新增状态码</span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">...</span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>引入了HTTP头的概念,无论是请求还是响应,允许传输其他信息,使协议更灵活以及更具扩展性</li>
</ul>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640.gif" alt="图片"></p>
<ul>
<li>在HTTP头的帮助下,具备了除了传输纯文本的HTML文件以外,还可以传输其他类型文档的能力(归功于<code>Content-Type</code> 头)</li>
</ul>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640-20220827174145258.gif" alt="图片"></p>
<p>HTTP0.9 规范大概只有一页,而HTTP/1.0在RFC-1945中定义的规范则足足有60页. 这说明HTTP已经成长为一个重要的工具</p>
<p>尽管HTTP/1.0已经有了很大的飞跃,但仍然存在许多必须解决的已知缺陷.</p>
<p>例如: 和TCP协议交互不良,没有充分考虑缓存等问题</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640-20220827174150746.gif" alt="图片"></p>
<p>HTTP/1.0每一次的通讯都需要建立并且断开连接,这无疑增加了无谓的通信开销</p>
<h2 id="HTTP-1-1-标准化的协议"><a href="#HTTP-1-1-标准化的协议" class="headerlink" title="HTTP/1.1-标准化的协议"></a>HTTP/1.1-标准化的协议</h2><p>文档RFC1945 定义了 HTTP/1.0 但是它是狭义的,并不是官方标准</p>
<p>所以实际运用起来非常的混乱.</p>
<p>从1995年开始,HTTP/1.0文档发布的下一年,就开始修订HTTP的第一个标准化版本</p>
<p>HTTP/1.1在1997年1月以RFC2068文件发布.</p>
<p>HTTP/1.1消除了大量歧义内容并引入了多项改进</p>
<ul>
<li><p>连接可以复用</p>
<p>节省了多次打开TCP连接加载网页文档资源的时间</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640-20220827174157262.gif" alt="图片"></p>
</li>
<li><p>增加管线化技术</p>
<p>允许第一个应答被完全发送前就发送第二个请求,以降低通信延迟</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640-20220827174200750.gif" alt="图片"></p>
</li>
<li><p>支持响应分块;</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640-20220827174204160.gif" alt="图片"></p>
</li>
<li><p>引入额外的缓存控制机制,</p>
<p>在HTTP cache-control 标头中引入了很多可以选择的选项</p>
</li>
<li><p>引入内容协商机制</p>
<p>包括语言,编码,类型等,并允许客户端和服务器之间约定以最合适的内容进行交换</p>
</li>
<li><p>能够使不同域名配置在同一个IP地址的服务器上</p>
</li>
</ul>
<p>一个典型的请求流程,所有请求都通过一个连接实现</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640-20220827174209420.gif" alt="图片"></p>
<p>由于HTTP的可扩展性–创建新的头部和方法是很容易的,</p>
<p>HTTP协议稳定使用了超过15年</p>
<p>期间不断对HTTP/1.1协议进行修订(RFC2616,RFC7230,RFC7235)</p>
<p>为HTTP/2.0做了十足的铺垫</p>
<h2 id="HTTP-2-0-为更优异的表现"><a href="#HTTP-2-0-为更优异的表现" class="headerlink" title="HTTP/2.0 为更优异的表现"></a>HTTP/2.0 为更优异的表现</h2><p>这些年来,网页逐渐变得复杂,甚至演变成了独有的应用,可见媒体的播放量,增进交互的脚本大小也增加了许多;更多的数据通过HTTP请求被传输.</p>
<p>在2010年到2015年,谷歌通过实践证明实验性的SPDY协议的可行性,这成为了后来HTTP/2协议的基础</p>
<p>HTTP/2在HTTP/1.1中有几处基本的不同</p>
<ul>
<li>HTTP/2 是二进制协议而不是文本协议,不再可读.头信息和数据体都是二进制(体积更小),并且统称为帧(frame)</li>
<li>这是一个复用协议,可以多路复用.并行的请求可以在同一个链接中处理,移除了HTTP/1.x中顺序和阻塞的约束</li>
</ul>
<h2 id="HTTP与HTTPS"><a href="#HTTP与HTTPS" class="headerlink" title="HTTP与HTTPS"></a>HTTP与HTTPS</h2><h3 id="为什么需要HTTPS"><a href="#为什么需要HTTPS" class="headerlink" title="为什么需要HTTPS?"></a>为什么需要HTTPS?</h3><p>HTTP协议在设计之初就没有充分考虑安全性的问题.所以基于HTTP的这些应用都承担着下列的风险</p>
<ol>
<li>使用明文(不加密)进行通信,内容可能被<strong>窃听</strong></li>
<li>不验证通信方的身份,通信方的身份可能是<strong>伪装</strong>的</li>
<li>无法验证信息的完整性,也就是说信息可能是被<strong>篡改</strong>过的</li>
</ol>
<p>HTTPS(HTTP over SSL)采取套接新一层安全套接字层(Secure Socket Layer,SSL)来解决网络传输的安全性问题</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640.png" alt="图片"></p>
<h3 id="如何防止被窃听"><a href="#如何防止被窃听" class="headerlink" title="如何防止被窃听?"></a>如何防止被窃听?</h3><p><code>非对称加密</code>:它把密码革命性地分成<strong>公钥</strong>和<strong>私钥</strong>，由于两个秘钥并不相同，所以称为非对称加密。</p>
<p>假设我们现在需要加密的字符是 <code>520</code>，我们<strong>加密</strong>的方法是把这个数乘以 <code>91</code>，并把结果的最后三位公布出来：</p>
<p><img src= "" data-lazy-src="https://mmbiz.qpic.cn/mmbiz_png/ibApg5SjhwicHOVibgALH3kzGcibDL2aUNSxxicrMgGWdIkko8cUFtuJqKI3aBzInACmSX53Pw7G03Qz5NZz0cn6R3w/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<p><strong>解密</strong>我们当然不能通过除以 <code>91</code> 来完成，而是通过 <code>x11</code>，取出结果后三位来还原：</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640-20220827174235830.png" alt="图片"></p>
<p>这是因为 <code>91*11=1001</code>，任何一个三位数乘以 <code>1001</code> 显然后三位是不会变的。这大概就是<strong>非对称加密的原理</strong>了，基于这个原理我们通信的双方就可以各自生成自己的公钥私钥并进行相对安全的通信了。</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640-20220827174246487.gif" alt="图片"></p>
<h3 id="如何验证对方身份"><a href="#如何验证对方身份" class="headerlink" title="如何验证对方身份"></a>如何验证对方身份</h3><p>面的过程看似无懈可击，但在 TCP/IP 的端到端的通信里，路途遥远</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640-20220827174249673.png" alt="图片"></p>
<p>如果在第二步的时候，信息被黑客截取，在严刑拷打之下知道了这是传输公钥的信息。那么完全可以自己生成一对密钥和公钥，冒充是彼此来传输自己的秘钥。</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typora640-20220827174255479.gif" alt="图片"></p>
<p>加密危机之后，又产生了信任危机。我们需要一个<strong>有公信力</strong>的组织来证明身份，这个问题就得到了解决。</p>
<p>这个可信的组织就是颁发 HTTPS 证书的组织 <strong>CA（Certificate Authority）</strong>。每次有客户端或者服务端想要公开自己的公钥时，都需要向 CA 做出申请，通过后 CA 会颁发一个与公开公钥绑定的数字证书。(了解更多证书)</p>
<p><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typoratypora640-20220827174259183.gif" alt="图片"></p>
<p>资料来源: 公众号Java3y</p>
</article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/http/">http</a><a class="post-meta__tags" href="/tags/%E7%BD%91%E7%BB%9C/">网络</a></div><div class="post_share"><div class="social-share" data-image="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/blog-common.png" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://s2.loli.net/2022/08/21/Ay5sujrkH1nLTwW.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="https://s2.loli.net/2022/08/21/Ay5sujrkH1nLTwW.jpg" alt="Wechat"/></a><div class="post-qr-code-desc">Wechat</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/39230a67/"><img class="prev-cover" src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/Java-cover.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">String相关</div></div></a></div><div class="next-post pull-right"><a href="/posts/bce68613/"><img class="next-cover" src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typoratimg.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">MySQL基础</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/c612f17c/" title="网络I&#x2F;O工作机制"><img class="cover" src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/blog-common.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-09</div><div class="title">网络I&#x2F;O工作机制</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="https://s2.loli.net/2022/08/20/hk9Zvq4YRsIcnrw.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">樱桃先生</div><div class="author-info__description">樱桃先生的博客小站</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">78</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">65</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/typoraWechat.jpeg"><i class="fab fa-weixin"></i><span>加为好友</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎光临樱桃小窝🍒～评论系统日常崩溃，没有找到长期稳定好用的组件。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Http"><span class="toc-number">1.</span> <span class="toc-text">Http</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFHttp"><span class="toc-number">1.1.</span> <span class="toc-text">什么是Http?</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HTTP-0-9-%E5%8D%95%E8%A1%8C%E5%8D%8F%E8%AE%AE"><span class="toc-number">1.2.</span> <span class="toc-text">HTTP&#x2F;0.9 单行协议</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HTTP-1-0-%E6%9E%84%E5%BB%BA%E5%8F%AF%E6%89%A9%E5%B1%95%E6%80%A7"><span class="toc-number">1.3.</span> <span class="toc-text">HTTP&#x2F;1.0 构建可扩展性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HTTP-1-1-%E6%A0%87%E5%87%86%E5%8C%96%E7%9A%84%E5%8D%8F%E8%AE%AE"><span class="toc-number">1.4.</span> <span class="toc-text">HTTP&#x2F;1.1-标准化的协议</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HTTP-2-0-%E4%B8%BA%E6%9B%B4%E4%BC%98%E5%BC%82%E7%9A%84%E8%A1%A8%E7%8E%B0"><span class="toc-number">1.5.</span> <span class="toc-text">HTTP&#x2F;2.0 为更优异的表现</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HTTP%E4%B8%8EHTTPS"><span class="toc-number">1.6.</span> <span class="toc-text">HTTP与HTTPS</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81HTTPS"><span class="toc-number">1.6.1.</span> <span class="toc-text">为什么需要HTTPS?</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E9%98%B2%E6%AD%A2%E8%A2%AB%E7%AA%83%E5%90%AC"><span class="toc-number">1.6.2.</span> <span class="toc-text">如何防止被窃听?</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E9%AA%8C%E8%AF%81%E5%AF%B9%E6%96%B9%E8%BA%AB%E4%BB%BD"><span class="toc-number">1.6.3.</span> <span class="toc-text">如何验证对方身份</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/4995/" title="顺德好友小聚"><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/blog-common.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="顺德好友小聚"/></a><div class="content"><a class="title" href="/posts/4995/" title="顺德好友小聚">顺德好友小聚</a><time datetime="2023-05-07T13:41:30.660Z" title="更新于 2023-05-07 21:41:30">2023-05-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/51438/" title="旅游归来的周末"><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/blog-common.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="旅游归来的周末"/></a><div class="content"><a class="title" href="/posts/51438/" title="旅游归来的周末">旅游归来的周末</a><time datetime="2023-05-06T06:06:54.933Z" title="更新于 2023-05-06 14:06:54">2023-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/34851/" title="与好友的广西之旅"><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/blog-common.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="与好友的广西之旅"/></a><div class="content"><a class="title" href="/posts/34851/" title="与好友的广西之旅">与好友的广西之旅</a><time datetime="2023-05-02T08:08:36.497Z" title="更新于 2023-05-02 16:08:36">2023-05-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/36794/" title="信用卡业务汇总"><img src= "" data-lazy-src="https://s2.loli.net/2022/08/22/kpfRHFaCWDTNAu1.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="信用卡业务汇总"/></a><div class="content"><a class="title" href="/posts/36794/" title="信用卡业务汇总">信用卡业务汇总</a><time datetime="2023-05-02T06:30:21.691Z" title="更新于 2023-05-02 14:30:21">2023-05-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/53652/" title="项目管理笔记"><img src= "" data-lazy-src="https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/blog-common.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="项目管理笔记"/></a><div class="content"><a class="title" href="/posts/53652/" title="项目管理笔记">项目管理笔记</a><time datetime="2023-05-02T06:29:26.948Z" title="更新于 2023-05-02 14:29:26">2023-05-02</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://ytblogpic.oss-cn-guangzhou.aliyuncs.com/blog-common.png')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 樱桃先生</div><div class="footer_custom_text">一切都是最好的安排</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><div class="js-pjax"><script>(()=>{
  const init = () => {
    twikoo.init(Object.assign({
      el: '#twikoo-wrap',
      envId: 'https://twikoo-seven-fawn.vercel.app',
      region: '',
      onCommentLoaded: function () {
        btf.loadLightbox(document.querySelectorAll('#twikoo .tk-content img:not(.tk-owo-emotion)'))
      }
    }, null))
  }

  const getCount = () => {
    const countELement = document.getElementById('twikoo-count')
    if(!countELement) return
    twikoo.getCommentsCount({
      envId: 'https://twikoo-seven-fawn.vercel.app',
      region: '',
      urls: [window.location.pathname],
      includeReply: false
    }).then(function (res) {
      countELement.innerText = res[0].count
    }).catch(function (err) {
      console.error(err);
    });
  }

  const runFn = () => {
    init()
    
  }

  const loadTwikoo = () => {
    if (typeof twikoo === 'object') {
      setTimeout(runFn,0)
      return
    } 
    getScript('https://cdn.jsdelivr.net/npm/twikoo/dist/twikoo.all.min.js').then(runFn)
  }

  if ('Twikoo' === 'Twikoo' || !true) {
    if (true) btf.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
    else loadTwikoo()
  } else {
    window.loadOtherComment = () => {
      loadTwikoo()
    }
  }
})()</script></div><script>window.addEventListener('load', () => {
  const changeContent = (content) => {
    if (content === '') return content

    content = content.replace(/<img.*?src="(.*?)"?[^\>]+>/ig, '[图片]') // replace image link
    content = content.replace(/<a[^>]+?href=["']?([^"']+)["']?[^>]*>([^<]+)<\/a>/gi, '[链接]') // replace url
    content = content.replace(/<pre><code>.*?<\/pre>/gi, '[代码]') // replace code
    content = content.replace(/<[^>]+>/g,"") // remove html tag

    if (content.length > 150) {
      content = content.substring(0,150) + '...'
    }
    return content
  }

  const getComment = () => {
    const runTwikoo = () => {
      twikoo.getRecentComments({
        envId: 'https://twikoo-seven-fawn.vercel.app',
        region: '',
        pageSize: 6,
        includeReply: true
      }).then(function (res) {
        const twikooArray = res.map(e => {
          return {
            'content': changeContent(e.comment),
            'avatar': e.avatar,
            'nick': e.nick,
            'url': e.url + '#' + e.id,
            'date': new Date(e.created).toISOString()
          }
        })

        saveToLocal.set('twikoo-newest-comments', JSON.stringify(twikooArray), 10/(60*24))
        generateHtml(twikooArray)
      }).catch(function (err) {
        const $dom = document.querySelector('#card-newest-comments .aside-list')
        $dom.innerHTML= "无法获取评论，请确认相关配置是否正确"
      })
    }

    if (typeof twikoo === 'object') {
      runTwikoo()
    } else {
      getScript('https://cdn.jsdelivr.net/npm/twikoo/dist/twikoo.all.min.js').then(runTwikoo)
    }
  }

  const generateHtml = array => {
    let result = ''

    if (array.length) {
      for (let i = 0; i < array.length; i++) {
        result += '<div class=\'aside-list-item\'>'

        if (true) {
          const name = 'data-lazy-src'
          result += `<a href='${array[i].url}' class='thumbnail'><img ${name}='${array[i].avatar}' alt='${array[i].nick}'></a>`
        }
        
        result += `<div class='content'>
        <a class='comment' href='${array[i].url}' title='${array[i].content}'>${array[i].content}</a>
        <div class='name'><span>${array[i].nick} / </span><time datetime="${array[i].date}">${btf.diffDate(array[i].date, true)}</time></div>
        </div></div>`
      }
    } else {
      result += '没有评论'
    }

    let $dom = document.querySelector('#card-newest-comments .aside-list')
    $dom.innerHTML= result
    window.lazyLoadInstance && window.lazyLoadInstance.update()
    window.pjax && window.pjax.refresh($dom)
  }

  const newestCommentInit = () => {
    if (document.querySelector('#card-newest-comments .aside-list')) {
      const data = saveToLocal.get('twikoo-newest-comments')
      if (data) {
        generateHtml(JSON.parse(data))
      } else {
        getComment()
      }
    }
  }

  newestCommentInit()
  document.addEventListener('pjax:complete', newestCommentInit)
})</script><script src="/js/jquery.min.js"></script><div class="aplayer no-destroy" data-id="5225697049" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="false" data-mini="true" data-lrctype="-1"> </div><script src="https://cdn.jsdelivr.net/gh/xiabo2/CDN@latest/fishes.js"></script><link rel="stylesheet" href="/css/utils.css"><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":170,"height":280},"mobile":{"show":true},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>